import {Component, OnInit} from '@angular/core';
import {ClipboardService} from "ngx-clipboard";
import {MatSnackBar} from "@angular/material/snack-bar";
import {icons} from "./names";

@Component({
  selector: 'app-material-icon',
  templateUrl: './material-icon.component.html',
  styleUrls: ['./material-icon.component.less'],
  providers: [MatSnackBar]
})
export class MaterialIconComponent implements OnInit {

  filter: string;
  group: string;

  testObj: { [propName: string]: any } = {};

  _icons = icons;

  constructor(private _clipboardService: ClipboardService,
              private  snackBar: MatSnackBar) {
  }

  ngOnInit() {

    this._clipboardService.copyResponse$.subscribe(() => {
      this.snackBar.open("复制成功！", "", {
        duration: 500,
      })
    })

  }

  get icons(): string[] {
    if (!this.filter) {
      return this._icons;
    } else {
      return this._icons.filter(value => value.startsWith(this.filter));
    }

  }

  copy(iClass) {

    let str = `<i class="material-icons">${iClass}</i>`;

    this._clipboardService.copy(str);

  }

  test() {

    if (!this.filter || !this.group) {
      return;
    } else {

      const icons = this.icons;

      let current: { [propName: string]: any } = {};
      current.group = this.group;
      current.icons = icons;
      this.testObj[this.filter] = current;

      let result = [];
      this._icons.forEach(value => {
        if (!icons.includes(value)) {
          result.push(value)
        }
      });
      this._icons = result;
    }
    console.log(this.testObj);
    console.log(this._icons);

  }

  // test() {
  //
  //   debugger;
  //   const result = {};
  //   for (let i = 0; i < this._icons.length; i++) {
  //     const name = this._icons[i];
  //
  //     const lineSplitIndex = name.indexOf('_');
  //     const spaceSplitIndex = name.indexOf(' ');
  //     let groupName = name;
  //
  //     if (lineSplitIndex>= 0 || spaceSplitIndex>= 0){
  //
  //       let split = null;
  //       if (lineSplitIndex != -1 && spaceSplitIndex != -1){
  //         split = lineSplitIndex < spaceSplitIndex ? '_' : ' ';
  //       }else if (lineSplitIndex != -1){
  //         split = '_';
  //       }else if (spaceSplitIndex != -1){
  //         split = ' ';
  //       }
  //
  //       if (split == null){
  //         continue;
  //       }
  //
  //       groupName = name.split(split)[0];
  //
  //     }
  //
  //     let group = result[groupName];
  //     if (!group) {
  //       group = {};
  //       group.names = [];
  //       group.des = '描述内容';
  //       result[groupName] = group;
  //     }
  //
  //     group.names.push(name);
  //   }
  //   console.log(JSON.stringify(result));
  // }

}
